#__*__coeding:utf-8__*__
#!/usr/share/python
'''
---------------------------------------------------------------- 
 版权所有。  
 
 文件名：
 文件功能描述： 
 author：
 时间：
--------------------------------------------------------------- 
'''

import os,datetime,time


class tools(object):

    @staticmethod
    def bubble_sort(args):
        """
        常规冒泡排序（由大到小）静态方法
        :param args:
        :return: list
        """
        for i in range(len(args)):
            for j in range(len(args) - 1):
                if args[j] < args[j + 1]:
                    args[j], args[j + 1] = args[j + 1], args[j]
        return args

    @staticmethod
    def sorted(s, args):
        """
        传入列表根据tuple对象的第二个索引值在bubbling静态方法中进行从大到小冒泡排序后重新建立args列表索引序列
        :param s
        #s 类对象的实例
        :param args:
         #args 实例化对象下方法返回的列表（列表内为tuple对象）
        :return: list[tuple]
        """
        j = []
        result = []
        for i in args:
            j.append(i[1])
        k = tools.bubble_sort(j)
        for a in k:
            for b in args:
                if b[1] == a:
                    result.append(b)
        return result

    # win&linux目录路径符转换
    @staticmethod
    def charte():
        '''
        To determine the current operating system type equipment
        :return: File system operator. nt return '\\'. nux return '/'
        '''
        if os.name == 'nt' or os.name == 'NT':
            charte = '\\'
        else:
            charte = '/'
        return charte

    def object_root_path(self):
        path=os.path.dirname(__file__)
        obj_path=self.directory_dispose(path)
        return obj_path

    @staticmethod
    def is_directory(path):
        '''
        To determine whether a directory exists
        :param path: F:\\xxx\\ or /use/xxx/
        :return: True or False
        '''
        if not os.path.exists(path):
            return True

    @staticmethod
    def path_combination(string,string2):
        '''
        String combination
        :param string: <tepy:str>
        :param string2: <tepy:str>
        :return: '%s%s%s'%(string,tools.charte(),string2)
        '''
        return '%s%s'%(string,string2)

    @staticmethod
    def directory_dispose(path,name=None):
        '''
        Directory at the next higher level
        :param path: F:\\xxx\\ or /use/xxx/
        :param name: None or file.o
        :return:path+name  /usr/file.o  <type:str>
        '''
        tmp = ''
        path_list=path.split(tools.charte())
        seq=len(path_list)
        path_list.remove(path_list[seq-1])
        charte=tools.charte()
        for i in range(seq-1):
            path_res=path_list[i]+charte
            tmp+=path_res
            path_result = tmp
        if name==None:
            return path_result
        else:
            return path_result+name

    @staticmethod
    def str_to_timestamp(str):
        return time.mktime(time.strptime(str,'%Y-%m-%d'))


    @staticmethod
    def timestamp_to_str(flot):
        return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(flot))

    @staticmethod
    def datetim_to_datestr(date):
        date_str=time.strptime(date,'%Y-%m-%d')
        return date_str
    @staticmethod
    def str_to_date(str):
        '''
        Time transformation, character format time into time format.
        example: < type: str > : 20150305 - > < type date > : 2015-03-05
        :param str: example: < type: str > : 20150305 - >
        :return: date_str   example:< type date > : 2015-03-05
        '''
        date = time.strptime(str, '%Y%m%d')
        date_str = time.strftime('%Y-%m-%d',date)
        return date_str

    @staticmethod
    def unstr_to_date(date):
        '''
        In contrast to the str_to_date.
        :param date: example: < type date > : 2015-03-05
        :return: date_str   example:< type: str > : 20150305 - >
        '''
        str= time.strftime(date, '%Y-%m-%d')
        date_str = time.strptime('%d%m%Y',str)
        return date_str

    @staticmethod
    def tuple_add_values(*args,i):
        '''
        To add value in the tuple
        :param args:
        :param i: list/tuple/dict
        :return:Add value after the tuples <type:tuple>
        '''
        if type(i) == list:
            tuple(i)
            tup = args + i
        elif type(i) == dict:
            tup = args + tuple(i.values())
        elif type(i) == tuple:
            tup = args + i
        else:
            lis = list(args)
            lis.append(i)
            tup = tuple(lis)
        return tup

    @staticmethod
    def get_time_now():
        return time.localtime(time.time())

    @staticmethod
    def datetime_now_strfull():
        timer= time.strftime('%Y-%m-%d %H:%M:%S',tools.get_time_now())
        return timer

    @staticmethod
    def date_now_strymd():
        date=time.strftime('%Y-%m-%d', tools.get_time_now())
        return date

    @staticmethod
    def date_now_timestamp():
        return time.mktime(tools.get_time_now())

    @staticmethod
    def datetim_to_strymd(datastr):
        date_str=str(datastr)
        date=time.strptime(date_str,'%Y-%m-%d %H:%M:%S')
        datestring=time.strftime('%Y-%m-%d',date)
        return datestring
